package com.aishua.fire.controller;

import java.util.List;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.aishua.fire.common.CookieUtils;
import com.aishua.fire.common.JsonArrayResult;
import com.aishua.fire.common.JsonObjectResult;
import com.aishua.fire.common.ValidateUtil;
import com.aishua.fire.common.anno.ModuleService;
import com.aishua.fire.common.anno.PermMapping;
import com.aishua.fire.common.constant.BigMenuConstants;
import com.aishua.fire.common.constant.ModuleConstants;
import com.aishua.fire.common.exp.ValidateException;
import com.aishua.fire.entity.account.Session;
import com.aishua.fire.entity.addressBook.AddressBookInfo;
import com.aishua.fire.entity.beOnDuty.AddBeOnDutyInfo;
import com.aishua.fire.entity.beOnDuty.BeOnDutyInfo;
import com.aishua.fire.entity.beOnDuty.BeOnDutyList;
import com.aishua.fire.entity.beOnDuty.ScheduleAddress;
import com.aishua.fire.service.BeOnDutyService;
import com.aishua.fire.service.LoginService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api(value = "值班管理-值班任务",tags = {"值班管理-值班任务管理模块接口"})
@RestController
@Slf4j
public class BeOnDutyManagerController
{

	@Autowired
	private BeOnDutyService onService;
	
	@Autowired
	private LoginService loginService;
	
	@Autowired
	private ValidateUtil validateUtil;
	
	@SuppressWarnings("rawtypes")
	@ModuleService(totalModule=BigMenuConstants.beOnDutyManager,moduleName=ModuleConstants.beOnDutyManager)
	@ApiOperation(value="值班管理-值班任务-新增值班任务")
	@PostMapping(value = {"/beOnDuty"} )
	public ResponseEntity<JsonObjectResult> add(@RequestBody @Valid AddBeOnDutyInfo info, BindingResult result) throws ValidateException
	{
		log.info("检查项目分类目录_新增分类目录信息:{}",info);
		validateUtil.respErrorMsg(result);//非空验证
		onService.save(info);
		
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);

		return ResponseEntity.ok(r);
    }
	
	@SuppressWarnings("rawtypes")
	@PermMapping(onlyLogin = true)
	@ModuleService(totalModule=BigMenuConstants.beOnDutyManager,moduleName=ModuleConstants.beOnDutyManager)
	@ApiOperation(value="值班管理-值班任务-查询值班任务列表（按照日期分组）")
	@GetMapping(value = {"/beOnDuty"} )
	public ResponseEntity<JsonArrayResult> select(@RequestParam(name = "date") String date)
	{
		log.info("值班管理-值班任务-查询值班任务列表（按照日期分组） :{}",date);
		List<BeOnDutyList> list = onService.selectDutyByDate(date);
		
		JsonArrayResult r = JsonArrayResult.makeSuccessResult(list, 0);

		return ResponseEntity.ok(r);
    }
	
	@SuppressWarnings("rawtypes")
	@PermMapping(noValidate=true)
	@ModuleService(totalModule=BigMenuConstants.beOnDutyManager,moduleName=ModuleConstants.beOnDutyManager)
	@ApiOperation(value="值班管理-值班任务-查询通讯录下拉框")
	@GetMapping(value = {"/beOnDuty/user"} )
	public ResponseEntity<JsonArrayResult> selectUserList()
	{
		log.info("组织结构管理_查询当前组织结构下所有员工:{}");
		
		List<AddressBookInfo> list = onService.selectUserList();
		JsonArrayResult r = JsonArrayResult.makeSuccessResult(list, 0);

		return ResponseEntity.ok(r);
    }
	
	/**
	 *  工作台 - 获取工作台值班任务
	 */
	@SuppressWarnings("rawtypes")
	@ModuleService(totalModule=BigMenuConstants.beOnDutyManager,moduleName=ModuleConstants.beOnDutyManager)
	@ApiOperation(value="工作台 - 获取当前登陆人员的工作台值班任务")
	@GetMapping(value = {"/selectBeOnDutyListToUser"} )
	public ResponseEntity<JsonArrayResult> getUserId(ServletRequest servletRequest) 
	{
		// （获取当前账号id接口）
		HttpServletRequest req = (HttpServletRequest) servletRequest;
		String sessionId = CookieUtils.getCookie(req, "sessionId");
		
		//根据sessionId获取当前账号id
		Session s = loginService.findBySessionId(sessionId);
		
		log.info("值班管理-值班任务-Session :{}",s);

		JsonArrayResult r = null;
		
		if(s != null)
		{
			List<BeOnDutyInfo> list = onService.selectBeOnDutyListToUser(s.getUser_id());
			r = JsonArrayResult.makeSuccessResult(list, 0);

		}
		else
		{
			r = JsonArrayResult.makeSuccessResult(null, 0);
		}
		return ResponseEntity.ok(r);
    }
	
	/**
	 *  工作台 - 获取工作台值班任务
	 */
	@SuppressWarnings("rawtypes")
	@PermMapping(noValidate=true)
	@ModuleService(totalModule=BigMenuConstants.beOnDutyManager,moduleName=ModuleConstants.beOnDutyManager)
	@ApiOperation(value="值班管理-值班任务-查询值班地址下拉框")
	@GetMapping(value = {"/getAddressSelectList"} )
	public ResponseEntity<JsonArrayResult> getAddressSelectList() 
	{
		List<ScheduleAddress> list = onService.getAddressSelectList();
		
		JsonArrayResult r = JsonArrayResult.makeSuccessResult(list, 0);
		return ResponseEntity.ok(r);
    }
}